#encoding=utf-8
import json
from scrapy import Request
from scrapy.selector import Selector
from scrapy.spiders import Spider
from maoyanmovie.items import MaoyanmovieItem

class MovieSpider(Spider):
    name = "maoyan.com"
    download_delay = 1  #延迟
    allowed_domain = ["maoyan.com" or "meituan.com" ]
    start_urls = ["http://api.meituan.com/mmdb/movie/v3/list/hot.json?ci=55&limit=12&token=fFF-QZUuKUUpnzoFN10LzY3Dzm8AAAAAmQIAAFNIswPB5PDzGQplr0S_7fKAYrX4PQmLcwQ864KImSAxWb-ZnJKCjv-z-fNO6h0eXA&__vhost=api.maoyan.com"]

    def parse(self, response):
        idresp = json.loads(response.body_as_unicode())
        idlist = idresp['data']['hot']  #'hot' is a list
        for ids in idlist:
            url = 'http://api.maoyan.com/mmdb/movie/v5/%s.json' % ids['id']
            yield Request(url, self.parse_item)

    def parse_item(self, response):
        jsonresp = json.loads(response.body_as_unicode())
        item = MaoyanmovieItem()
        item['name'] = jsonresp['data']['movie']['nm']
        item['genres'] = jsonresp['data']['movie']['cat']  
        item['dur'] = jsonresp['data']['movie']['sc'] 
        item['country'] = jsonresp['data']['movie']['src'] 
        item['release'] = jsonresp['data']['movie']['pubDesc']
        item['score'] = jsonresp['data']['movie']['sc']
        item['descr'] = jsonresp['data']['movie']['dra']
        yield item
